//所有待办和已办略况展示
var app=getApp()
var utils = require('../../utils/util.js')
Page({
  data: {//setData设置的数据不必一定要在此处预先定义
    workflowList: [],
    historicList: [],
    todoBusinessList: [],
    businessList: [],
    activeNum:0,
  },
 
  gopage0: function (event) { 
    const id = event.currentTarget.dataset.select;
    this.setData({
      activeNum: id
    });
    this.getTodoListJSON();
  },

  gopage1: function (event) {
    const id = event.currentTarget.dataset.select;
    this.setData({
      activeNum: id
    });
    this.getHistoricJSON();
  },

  gopage2: function (event) {
    const id = event.currentTarget.dataset.select;
    this.setData({
      activeNum: id
    });
    this.getTodoBusinessTaskJSON();
  },

  gopage3: function (event) {
    const id = event.currentTarget.dataset.select;
    this.setData({
      activeNum: id
    });
    this.getBusinessTaskJSON();
  },


 
  /**
   * 生命周期函数--监听页面加载
   * this指向问题，因为是在回调函数里面获取数据的，然后需要放到data对象里面，所以需要提前保存this对象的地址
   */
  onLoad: function () {
    console.log("onload...")
    var that = this;
    if (app.globalData.user == null) {//未登录
      console.log(" not login")
      wx.switchTab({
        url: '../usercenter/user',  
      })
    }else{
      wx.showLoading({
        title: '加载中',
      })
      that.setData({
          workflowList: that.getTodoListJSON(),
          historicList: that.getHistoricJSON(),
          todoBusinessList: that.getTodoBusinessTaskJSON(),
          businessList: that.getBusinessTaskJSON()
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 3000) 
    }
  },

/**
 * 获取待办流程
 */
  getTodoListJSON: function(){
    var that = this,
      url = app.GLOBAL_CONFIG.commonUrl + app.GLOBAL_CONFIG.todoListJSONUrl,
      cookie = app.globalData.cookie
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000, 
      })
    } else {
      utils.getRequest(url, cookie, app,that, function (res, that){
        that.setData({
          workflowList: res.data.rows,
        })
      },
      function(res){
        wx.showToast({
          title: '获取数据失败，请稍后重试',
          image: '../../images/fail.png', 
          duration: 2000
        })
      })
    }   

    
  },

/**
 * 获取已办流程
 */
  getHistoricJSON: function(){
    var that = this,
      url = app.GLOBAL_CONFIG.commonUrl + app.GLOBAL_CONFIG.historicJSONUrl,
      cookie = app.globalData.cookie
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    } else {  
      utils.getRequest(url, cookie, app,that, function (res, that) {
        that.setData({
          historicList: res.data.rows,

        })
      },function(res){
        wx.showToast({
          title: '获取数据失败，请稍后重试',
          image: '../../images/fail.png',
          duration: 2000
        })
      })  
    }  
  },

/**
 * 获取待办任务
 */
  getTodoBusinessTaskJSON: function(){
    var that = this,
      url = app.GLOBAL_CONFIG.commonUrl + app.GLOBAL_CONFIG.todoBusinessTaskUrl,
      cookie = app.globalData.cookie
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    } else {
      utils.getRequest(url, cookie, app,that, function (res, that) {
        that.setData({
          todoBusinessList: res.data.rows,
        })
      },function(res){
        wx.showToast({
          title: '获取数据失败，请稍后重试',
          image: '../../images/fail.png',
          duration: 2000
        })
      })
    }
  },

/**
 * 获取已办任务
 */
  getBusinessTaskJSON: function(){
    var that = this,
      url = app.GLOBAL_CONFIG.commonUrl + app.GLOBAL_CONFIG.historicBusinessTaskUrl,
      cookie = app.globalData.cookie
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    } else {
      utils.getRequest(url, cookie, app,that, function (res, that) {
        that.setData({
          businessList: res.data.rows,

        })
      },function(res){
        wx.showToast({
          title: '获取数据失败，请稍后重试',
          image: '../../images/fail.png',
          duration: 2000
        })
      }) 
    }  
  },

  itemtap: function(e){
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    }else{
      var that = this,
        id = e.currentTarget.id,
        flowDetail = that.data.workflowList[id];//workflows
      if(flowDetail.status == 'claim'){
        wx.showModal({
          title: '提示',
          content: '是否确定签收',
          success: function (res) {
            if (res.confirm) {
              var url = app.GLOBAL_CONFIG.commonUrl + app.GLOBAL_CONFIG.signTaskUrl + '?taskId=' + flowDetail.taskId,
                cookie = app.globalData.cookie
              utils.postRequest(url, cookie,app, that, function (resu,that) {
                if(resu.data == "success"){
                  that.onLoad();
                }
              },
              function (res) {
                  wx.showModal({
                    title: '签收失败', content: '请检查设置是否正确！', showCancel: false, success: function (res) {
                      return;
                    }
                  });
                })
            }else{
              return;
            }
          }
        })


      }else{
        var url = that.formatUrl(flowDetail);
        wx.navigateTo({
          url: url
        })
      }
   
    }
  },

  formatUrl: function (flowDetail){
    var procDefName = flowDetail.procDefName,
      taskName = flowDetail.taskName,
      applyUserName = flowDetail.applyUser.name,
      taskCreateDate = flowDetail.taskCreateDate,
      taskId = flowDetail.taskId,
      taskDefKey = flowDetail.taskDefKey,
      procInsId = flowDetail.procInsId,
      procDefId = flowDetail.procDefId,
      assignee = flowDetail.assignee,
      status = flowDetail.status; 
    return "../todoDetails/todoDetails?taskId=" + taskId + "&taskName=" + taskName + "&taskDefKey=" + taskDefKey + "&procInsId=" + procInsId + "&procDefId=" + procDefId + "&status=" + status + "&procDefName=" + procDefName + "&applyUserName=" + applyUserName + "&taskCreateDate=" + taskCreateDate + "&assignee=" + assignee
  },

/**
 * 已完成流程查看
 */
  historicItemtap:function(e){
    console.log("点击了查看已办流程")
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    } else {
      var that = this,
        id = e.currentTarget.id,
        flowDetail = that.data.historicList[id],
        procInsId = flowDetail.procInsId,
        processDefinitionId = flowDetail.processDefinitionId,
        executionId = flowDetail.executionId
      wx.navigateTo({
        url: '../historicFlow/historicFlow?procInsId=' + procInsId + "&processDefinitionId=" + processDefinitionId + "&executionId" + executionId,
      }) 
    }
  
  },
/** 
 * 业务待办
 */
  businessItemtap:function(e){
    console.log("点击了业务待办详情")
    if (utils.checkNetWorkStatus() == false) {
      console.log('网络错误');
      wx.showToast({
        title: '网络错误请检查',
        image: '../../images/fail.png',
        duration: 2000,
      })
    } else {
      var that = this,
        id = e.currentTarget.id,
        status = e.currentTarget.dataset.status,
        flowDetail = null
      if (status != undefined && status == "2") {
        flowDetail = that.data.businessList[id]
      } else {
        flowDetail = that.data.todoBusinessList[id]
      }
      var businessTaskId = flowDetail.id,
        title = flowDetail.title,
        initiatorUserName = flowDetail.initiatorUser.name,
        initiateTime = flowDetail.initiateTime;
      wx.navigateTo({
        url: "../todoBusinessDetails/todoBusinessDetails?businessTaskId=" + businessTaskId
      })

    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { 
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onshow...")
    var that = this;
    that.onLoad();
  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    console.log("调用页面下拉的onLoad")
    this.onLoad();
    wx.stopPullDownRefresh();
  },
  refreshPage: function () {
    console.log("调用页面下拉的refreshPage")
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏  
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
